package cn.hy;


import cn.hy.properties.DataBase;
import cn.hy.properties.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;

/**
 *  属性加载优先级
 * https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config
 *
 *  https://javadoop.com/post/spring-properties
 *
 *  Spring 中属性变量的读取
 */
@Slf4j
@SpringBootApplication
// @ImportResource(locations = {"classpath:spring-bean.xml"})
public class Main implements ApplicationRunner {


    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }

    @Value("${hy.cn.url}")
    private String url;

    @Value("${name}")
    private String name;

    @Resource
    private Environment environment;

    @Resource
    private DataBase dataBase;

    @Resource
    private PropertiesUtil propertiesUtil;




    @Override
    public void run(ApplicationArguments args) throws Exception {

        String password = propertiesUtil.getPropertiesValue("hy.database.password");
        log.info("password: {}",password);
        String parseTemplate = propertiesUtil.parseTemplate("我的密码是:${hy.database.password}");
        log.info("parseTemplate: {}",parseTemplate);

        log.info("value: {}",url);
        log.info("name: {}",name);
        String property = environment.getProperty("hy.cn.url");
        log.info("environment : {} ",property);

        log.info("hy properties: {}", environment.getProperty("name")); // null
        log.info("hy2 properties: {}", environment.getProperty("name2"));

        log.info("database: {}",dataBase.toString());


    }
}